<!doctype html>
<html lang="en" ng-app="hd">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="../org/angular.min.js"></script>
</head>
<body>
<div ng-controller="ctrl">
    标题：<input type="text" ng-model="title"> {{error}}
</div>
<script>
    var m = angular.module('hd', []);
    m.controller('ctrl', ['$scope', function ($scope) {
        $scope.title = '';
        $scope.$watch('title', function (n, o) {
            $scope.error = n.length >= 4 ? '标题的长度不能大于4' : '';
            $scope.title = $scope.title.substr(0, 4);
        });
    }]);

</script>
</body>
</html>


